    {  Petrova Tatjana
     2013 / 01 / 16
     This is visualisation of sort.  }
unit Bubble;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

const
  n =5;
  w=50;
  h=56;
  x=30;
  y=30;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private

  public
    procedure Draw;
  end;

var
  Form1: TForm1;
  arr : array  of integer;
  step,step2 : integer;
  arr1: array of TPanel;



implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
  randomize;
  Setlength(arr,n);
  Setlength(arr1,n);
  for i := 0 to (n-1) do
    arr[i]:=random(100);
  for i := 0 to (n-1) do
  begin
      arr1[i]:=  TPanel.Create(Form1);
      arr1[i].Width := x;
      arr1[i].Height := y;
      arr1[i].Parent := Form1;
      arr1[i].Top := h;
      arr1[i].Left := w*(i mod n);
  end;
  Timer1.Enabled:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Draw;
  Timer1.Enabled:=True;
end;
procedure TForm1.Draw;
var i : integer;
begin
    for i := 0 to (n-1) do
    begin
      arr1[i].Color := clWhite;
      if(i=step2) or (i=step2+1) then
        arr1[i].Color := clRed;
      arr1[i].Caption:=IntToStr(arr[i]);
    end;
    Sleep(10);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var k : integer;
begin
  Draw;
  if (arr[step2] > arr[step2+1]) then
  begin
    k := arr[step2];
    arr[step2] := arr[step2+1];
    arr[step2+1]:=k;
    Draw;
    sleep(10);
  end;
  step2 :=step2+1;
  if (step2=n-1-step)then
  begin
    step2:=0;
    step := step+1;
  end;
  if (step=n-1)then
    Timer1.Enabled := False;
end;

end.


